{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "разбор ДЗ 8.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.12"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "XQgRG9KlCzoP"
      },
      "source": [
        "# Урок 8"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5bpC3w-ECzoS"
      },
      "source": [
        "# Сингулярное разложение матриц"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "88Mbd_tGCzso"
      },
      "source": [
        "## Практическое задание"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "aoo5c_Q0Czsp"
      },
      "source": [
        "1. Найти с помощью NumPy SVD для матрицы\n",
        "\n",
        "$$\\begin{pmatrix}\n",
        "1 & 2 & 0\\\\ \n",
        "0 & 0 & 5\\\\ \n",
        "3 & -4 & 2\\\\ \n",
        "1 & 6 & 5\\\\ \n",
        "0 & 1 & 0\n",
        "\\end{pmatrix}.$$\n",
        "\n",
        "\n",
        "2. Для матрицы из предыдущего задания найти с помощью NumPy и вручную:\n",
        "\n",
        "    а) евклидову норму;\n",
        "    \n",
        "    б) норму Фробениуса."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6cKhRuqVBdR7"
      },
      "source": [
        "__Решение__\n",
        "__1.__"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "aVQNdY3HBdR9"
      },
      "source": [
        "import numpy as np\n",
        "np.set_printoptions(precision=2, suppress=True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pdDhz_ErBdR_",
        "outputId": "45bb14bb-1f06-4e80-bbcc-bab200dfb3ef"
      },
      "source": [
        "A = np.array([[1,2,0],\n",
        "              [0,0, 5],\n",
        "              [3,-4,2],\n",
        "              [1,6,5],\n",
        "             [0,1,0]])\n",
        "print(f'Матрица A:\\n{A}')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Матрица A:\n",
            "[[ 1  2  0]\n",
            " [ 0  0  5]\n",
            " [ 3 -4  2]\n",
            " [ 1  6  5]\n",
            " [ 0  1  0]]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NxjMq-5rBdSD"
      },
      "source": [
        "U, s, W = np.linalg.svd(A)\n",
        "\n",
        "# Транспонируем матрицу W\n",
        "V = W.T\n",
        "\n",
        "# s - список диагональных элементов, его нужно привести к виду диагональной матрицы для наглядности\n",
        "Sigma = np.zeros_like(A, dtype=float)\n",
        "Sigma[np.diag_indices(min(A.shape))] = s"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hneMFHQfBdSE",
        "outputId": "c4ec085f-8ae0-4f05-e9ff-ec1c336e5618"
      },
      "source": [
        "print(f'Матрица U:\\n{U}')\n",
        "print(f'Матрица Sigma:\\n{Sigma}')\n",
        "print(f'Матрица V:\\n{V}')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Матрица U:\n",
            "[[ 0.17  0.16 -0.53 -0.8  -0.16]\n",
            " [ 0.39 -0.53  0.61 -0.43  0.03]\n",
            " [-0.14 -0.82 -0.52  0.14  0.07]\n",
            " [ 0.89  0.06 -0.25  0.38 -0.06]\n",
            " [ 0.08  0.11 -0.08 -0.11  0.98]]\n",
            "Матрица Sigma:\n",
            "[[8.82 0.   0.  ]\n",
            " [0.   6.14 0.  ]\n",
            " [0.   0.   2.53]\n",
            " [0.   0.   0.  ]\n",
            " [0.   0.   0.  ]]\n",
            "Матрица V:\n",
            "[[ 0.07 -0.37 -0.93]\n",
            " [ 0.72  0.67 -0.21]\n",
            " [ 0.69 -0.65  0.31]]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "H_JgWsT3BdSF"
      },
      "source": [
        "### Норма Евклидову "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "g_Y7N1I0BdSG",
        "scrolled": true,
        "outputId": "4f5b5922-e761-4173-bbf5-ffd323e9e178"
      },
      "source": [
        "np.linalg.norm(A, ord=2)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "data": {
            "text/plain": [
              "8.824868854820444"
            ]
          },
          "execution_count": 5,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hawxELTtBdSI"
      },
      "source": [
        "### Норма Фробениуса"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MDlnUZDlBdSJ",
        "scrolled": true,
        "outputId": "1cd854e2-a2b7-42d1-fd3c-e070791cd624"
      },
      "source": [
        "(122)**(1 / 2)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "data": {
            "text/plain": [
              "11.045361017187261"
            ]
          },
          "execution_count": 6,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fnQ5pO_fBdSM",
        "scrolled": true,
        "outputId": "fcbde9d7-b02b-47c4-cb64-ad111e6ad094"
      },
      "source": [
        "(8.82**2 + 6.14**2 + 2.53**2)**(1 / 2)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "data": {
            "text/plain": [
              "11.04051176350082"
            ]
          },
          "execution_count": 7,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Js9JuTrqBdSN",
        "outputId": "ae788578-596c-499e-9e1f-747e29c06507"
      },
      "source": [
        "np.linalg.norm(A, ord=None)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "data": {
            "text/plain": [
              "11.045361017187261"
            ]
          },
          "execution_count": 8,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HYwqP2YwBdSP",
        "outputId": "45fb1d26-841a-44bf-a430-3541dca449dd"
      },
      "source": [
        "np.linalg.norm(A, ord='fro')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "data": {
            "text/plain": [
              "11.045361017187261"
            ]
          },
          "execution_count": 9,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        }
      ]
    }
  ]
}